<?php

/**
 * project actions.
 *
 * @package    mysmartmanager
 * @subpackage project
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class projectActions extends sfActions
{
  public function executeBudgetcost(sfWebRequest $request)
  {     
    $this->projects = Doctrine_Core::getTable('Project')->getBudgetCostComparison();
  }
  public function executeAssignresource(sfWebRequest $request)
  {    
   	$this->project = $this->getUser()->getAttribute('selectedProject');
    $this->projects = Doctrine::getTable('Projectresource')
	  ->createQuery('p')
	  ->where('p.projectid = ?', $this->project->getProjectid())
      ->execute();
	$this->resources = Doctrine_Core::getTable('Resource')->getResourcesNotWorkingInProject($this->project->getProjectid());
	//$this->redirect('project/index');
  }
  
  public function executeIndex(sfWebRequest $request)
  {    
	if (!sfContext::getInstance()->getUser()->getResourceId())
	  $this->redirect('resource/new');
	$projectsWithResourceCountTaskCountMilestoneCount = Doctrine_Core::getTable('Project')->getResourceCountTaskCountMilestoneCount();
	if (count($projectsWithResourceCountTaskCountMilestoneCount) > 0)
	  $this->projects = $projectsWithResourceCountTaskCountMilestoneCount;
	else
	  $this->redirect('task/index');
/*	$selectedProject = Doctrine::getTable('Project')
	                   ->createQuery('p')
					   ->where('createdby = ?', $this->getUser()->getResourceId())
					   ->limit(1)
					   ->execute();
					   */
	//$this->getUser()->setAttribute('selectedProject', $this->getUser()->getGuardUser()->getResource()->getProjectsOfLoggedInUser()->get(0));
//	$this->getUser()->setFlash('error', 'Please select one of the Projects Below.');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->project = $this->getRoute()->getObject();	
	$this->getUser()->setAttribute('selectedProject', $this->project);
	//$resourceId = sfContext::getInstance()->getUser()->getGuardUser()->getResource()->getResourceid();
	//$this->project = Doctrine::getTable('Project')->find(array($request->getParameter('projectid');
    //$this->forward404Unless($this->project);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ProjectForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
 
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ProjectForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($project = Doctrine::getTable('Project')->find(array($request->getParameter('projectid'))), sprintf('Object project does not exist (%s).', $request->getParameter('projectid')));
    $this->form = new ProjectForm($project);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($project = Doctrine::getTable('Project')->find(array($request->getParameter('projectid'))), sprintf('Object project does not exist (%s).', $request->getParameter('projectid')));
    $this->form = new ProjectForm($project);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($project = Doctrine::getTable('Project')->find(array($request->getParameter('projectid'))), sprintf('Object project does not exist (%s).', $request->getParameter('projectid')));
    $project->delete();

    $this->redirect('project/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
 
    if ($form->isValid())
    {
      $project = $form->save();
	  if($form->getValue("projectid") == 0) {
        $this->getUser()->setFlash('notice', 'Project created successfully.');
	  } else {
	    $this->getUser()->setFlash('notice', 'Project updated successfully.');
	  }
      $this->redirect('project/edit?projectid='.$project->getProjectid());
	  //$this->redirect(url_for('project_show', $project, true));
    }
  }
}
